package es.ats.codington.web.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class VisitorRegistrationServlet
 */
@WebServlet("/VisitorRegistrationServlet")
public class VisitorRegistrationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public VisitorRegistrationServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorRegistration.jsp");  
		rd.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");   
	    
	    HttpSession session=request.getSession(true);
	    
	    
	    Visitor u =new Visitor();
	    
	    u.setFirstName(request.getParameter("firstname"));
	    u.setLastName(request.getParameter("lastname"));
	    u.setEmail(request.getParameter("emailAddress"));
	    u.setPhone(	Long.parseLong(request.getParameter("phone"))	);
	    u.setDni(request.getParameter("dni"));
	    u.setAddress(request.getParameter("addres"));
	    u.setVisitorName(request.getParameter("username"));
	    u.setPassword(request.getParameter("password"));
	        

	    VisitorService vs = new VisitorService();
	    
	    try
	    {
	    	
	    	if(vs.registerNewVisitor(u)){
	    		session.setAttribute("idVisitor", vs.getIdVisitor(u));
	    		session.setAttribute("visitorName",u.getVisitorName());
	    		session.setAttribute("password",u.getPassword());
	    		session.setAttribute("firstName",u.getFirstName());
	    		session.setAttribute("lastName",u.getLastName());
	    		session.setAttribute("email",u.getEmail());
	    		session.setAttribute("phone",u.getPhone());
	    		session.setAttribute("address", u.getAddress());
	    		session.setAttribute("dni", u.getDni());
	    		session.setAttribute("isAdmin",0);
	    		
	    		
	    		ViewEventsofUserServlet view=new ViewEventsofUserServlet();
    			view.doPost(request, response);
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_myPortal.jsp");  
    			rd.forward(request,response);
	    	}
	    	else
	    	{
	    		RequestDispatcher rd=request.getRequestDispatcher("NewCodington_visitorLogin.jsp");  
    			rd.forward(request,response);
	    	}
	    	
	    	
	    }
	    catch(Exception eee){
	    	eee.getMessage();	
	    }
		
		
	}

}
